<template>
  <div ref="chart" :style="{ width: '100%', height: '200px' }"></div>
</template>


<script lang="ts" setup>
import * as echarts from 'echarts'
import {  ref,  onMounted, onBeforeUnmount} from "vue";
const chart = ref(null)

const option = ref( {
  tooltip: {
    formatter: '{a} <br/>{b} : {c}%'
  },
  series: [
    {
      name: 'Pressure',
      type: 'gauge',
      detail: {
        formatter: '{value}'
      },
      data: [
        {
          value: 50,
          name: 'SCORE'
        }
      ]
    }
  ]
})

let myChart:any 

const initChart = () => {
  myChart.resize()
}

onMounted(() => {
  // mounted中初始化charts
  myChart = echarts.init(chart.value as unknown as HTMLElement)
  myChart.setOption(option.value)
  // resize事件
  window.addEventListener("resize", initChart)
})


onBeforeUnmount(() => {
  // 销毁resize事件
  window.removeEventListener('resize', initChart)
})

</script>